<%
	/**
	 * positionSelect 外部调用的地图定位对话框
	 * 
	 * @author  李荣华
	 * @version v1.0
	 * @date    2017-11-30
	 * @since   JSP 2.3.1
	 */
%>
<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib prefix="at" uri="http://www.jketing.com/at"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>地图定位</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<meta content="no-cache" http-equiv="Pragma" />
<meta content="no-cache" http-equiv="Cache-Control" />
<meta content="0" http-equiv="Expires" />
<meta name="GENERATOR" content="MSHTML 8.00.6001.18854" />
<link rel="stylesheet" id="list" type="text/css" href="<at:url css="dialog.css"/>" />
<script type="text/javascript" src="${pageContext.request.contextPath }/scripts/jquery.js"></script>	
<script type="text/javascript" src="${pageContext.request.contextPath }/scripts/common.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=QLTszFSAUztb6vO2pkHbxRg4qp3soF1l"></script>
<base target="_self" />
</head>
<body>
	<form action="" name="queryForm" method="post">	
	<input type="hidden" id="longitude" />	
	<input type="hidden" id="latitude" />		
		<!--搜索-->
		<div class="queryTab">
			<div style="width: 80%">
				地址:<input id="address"  type="text" style="width:  90%"/>
				<a href="#" onclick="searchByStationName();">查找</a>
			</div>
		</div>
		  <!--地图展示区-->
		  <div class="queryContent" id="container">
		  </div>
		  <!--底部-->
		  <div class="bottom">
			<div onclick="doCancel();">
				取消
			</div>
			<div onclick="okClick();">
				确定
			</div>
	  	</div>
	</form>
</body>
<script type="text/javascript">
	var myObject = window.dialogArguments;
    var map = new BMap.Map("container");
    //map.centerAndZoom("宁波", 12);
    var point = new BMap.Point(116.331398,39.897445);
	map.centerAndZoom(point,12);
	// 创建地址解析器实例
	var myGeo = new BMap.Geocoder();
	
    //获取地址信息，设置地址label
    function getAddress(marker){
        myGeo.getLocation(marker.getPosition(), function(rs){
            var addComp = rs.addressComponents;
            var address =  addComp.province +  addComp.city + addComp.district + addComp.street + addComp.streetNumber;//获取地址
            //var addr = document.getElementById("address").value;
            document.getElementById("longitude").value = marker.getPosition().lng;
            document.getElementById("latitude").value = marker.getPosition().lat;
            document.getElementById("address").value = address;
            //画图 ---》显示地址信息
            var label = new BMap.Label(address,{offset:new BMap.Size(20,-10)});
            map.removeOverlay(marker.getLabel());//删除之前的label 
            marker.setLabel(label);
         });
    }
    map.enableScrollWheelZoom();    //启用滚轮放大缩小，默认禁用
    map.enableContinuousZoom();    //启用地图惯性拖拽，默认禁用
    map.addControl(new BMap.NavigationControl());  //添加默认缩放平移控件
    map.addControl(new BMap.OverviewMapControl()); //添加默认缩略地图控件
	map.addControl(new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP,BMAP_HYBRID_MAP]}));
    var localSearch = new BMap.LocalSearch(map);
    localSearch.enableAutoViewport(); //允许自动调节窗体大小
	
    function searchByStationName() {
	    map.clearOverlays();//清空原来的标注
	    var keyword = document.getElementById("address").value;
	    localSearch.setSearchCompleteCallback(function (searchResult) {
	        var poi = searchResult.getPoi(0);
	        map.centerAndZoom(poi.point, 18);
	        var marker = new BMap.Marker(new BMap.Point(poi.point.lng, poi.point.lat));  // 创建标注，为要查询的地方对应的经纬度
	        map.addOverlay(marker);
			marker.enableDragging();
			getAddress(marker);
		});
	    localSearch.search(keyword);
	} 
    map.addEventListener("click", function(e){
        var point = new BMap.Point(e.point.lng, e.point.lat);
        map.clearOverlays();//清空原来的标注
        var marker = new BMap.Marker(point);  // 创建标注，为要查询的地方对应的经纬度
        map.addOverlay(marker);
		marker.enableDragging();
		getAddress(marker);
    });
    
    function currentPosition(){
    	var geolocation = new BMap.Geolocation();
	    geolocation.getCurrentPosition(function (r) {
	        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
	        	map.clearOverlays();//清空原来的标注
	        	alert(r.point.lng + " ， " + r.point.lat);
	            map.centerAndZoom(r.point, 13);
	            var marker = new BMap.Marker(r.point);  // 创建标注，为要查询的地方对应的经纬度
	            map.addOverlay(marker);
	    		marker.enableDragging();
	    		getAddress(marker);
	            //showPosition(pt);
	            //传参数给地图按钮
	            //document.getElementById("alinkMap").href += "&x="+r.point.lng+"&y="+r.point.lat;
	            //document.getElementById("alinkMap").style.display="block";
	        }
	    });
    }
    
   // 将地址解析结果显示在地图上,并调整地图视野
	if(myObject.longitude.value!=''){
		map.clearOverlays();//清空原来的标注
		var point = new BMap.Point(myObject.longitude.value,myObject.latitude.value);
        map.centerAndZoom(point, 18);
        var marker = new BMap.Marker(point);  // 创建标注，为要查询的地方对应的经纬度
        map.addOverlay(marker);
		marker.enableDragging();
		getAddress(marker);
		//document.getElementById("address").value = '${addr}';
		//searchByStationName();
	}
	/*myGeo.getPoint(addr, function(point){
		if (point) {
			map.centerAndZoom(point, 16);
			 var marker = new BMap.Marker(point);  // 创建标注，为要查询的地方对应的经纬度
			map.addOverlay(marker);
			marker.enableDragging();
			$("#btn").click(function(){
				getAddress(point);
				//close();
			});
		}
	}, "中国");*/
	//百度地图WebAPI 坐标转地址
    function showPosition(r) {
        // ak = appkey 访问次数流量有限制
        var url = 'http://api.map.baidu.com/geocoder/v2/?ak=QLTszFSAUztb6vO2pkHbxRg4qp3soF1l&callback=?&location=' + r.point.lat + ',' + r.point.lng + '&output=json&pois=1';
        $.getJSON(url, function (res) {
            $("#msg").html(url);
            alert(res.result.addressComponent.city);
        });
    }
	
	function okClick() 
    {
	    if ( document.getElementById("longitude").value != "" ) 
	    {
	    	myObject.longitude.value= document.getElementById("longitude").value;
			myObject.latitude.value= document.getElementById("latitude").value;
			myObject.addr.value=document.getElementById("address").value;
			window.close();
	    }
        else
        {  
            alert('你没有选择定位！');
        }
    }
	
	function doCancel() {
		window.close();
	}
	    
</script>
</html>
